package com.hx.er.action.manager.msg;

import org.springframework.beans.factory.annotation.Autowired;

import com.hx.er.action.base.ErManagerAction;
import com.hx.er.bean.detail.MessageDetailBean;
import com.hx.er.common.mail.IMailSender;
import com.hx.er.common.mail.MailInfo;
import com.hx.er.common.mail.SimpleMailSender;
import com.hx.er.common.system.Constants;
import com.hx.er.common.system.ErProperties;
import com.hx.er.common.util.StringUtil;
import com.hx.er.service.manager.IManagerMsgService;

public class ManagerMsgAddAction extends ErManagerAction {
	// 序列化ID
	private static final long serialVersionUID = -1127474451530316813L;

	// 页面消息
	public static final String MSG_CONTENT_LENGTH_MSG = "manager.msgList.message.add.length.msgContent";

	private MessageDetailBean msgDetailBean;

	@Autowired
	private IManagerMsgService managerMsgService;

	public String init() {
		this.initPage();
		return SUCCESS;
	}

	public String save() {
		if (!this.checkParams()) {
			return INPUT;
		}

		this.msgDetailBean.setRestId(super.restId);
		this.msgDetailBean.setSubmitUserId(super.getUserInfoLogin().getUserId());
		this.msgDetailBean.setCreateUserId(super.getUserInfoLogin().getUserId());
		this.msgDetailBean.setUpdateUserId(super.getUserInfoLogin().getUserId());

		try {
			// 将消息数据插入数据库
			this.managerMsgService.insertMsgWithoutId(this.msgDetailBean);
			this.addActionMessage(super.getText(OPERATION_SUCCESS_MSG));
			
			// 异步发送邮件
			Thread mThread = new Thread(new MailThread(this.msgDetailBean));
			mThread.start();
			
			return SUCCESS;
		} catch (Exception e) {
			this.initPage();
			this.addActionError(super.getText(OPERATION_FAILURE_MSG));
			return INPUT;
		}
	}

	private void initPage() {

	}

	private boolean checkParams() {
		// 消息长度证
		if (!StringUtil.checkLength(this.msgDetailBean.getMsgContent(), 0, 512)) {
			this.addFieldError("msg.msgContent", super.getText(MSG_CONTENT_LENGTH_MSG));
		}

		// 错误信息数量
		int errCount = this.getActionErrors().size() + this.getFieldErrors().size();

		return errCount == 0;
	}

	public MessageDetailBean getMsgDetailBean() {
		return msgDetailBean;
	}

	public void setMsgDetailBean(MessageDetailBean msgDetailBean) {
		this.msgDetailBean = msgDetailBean;
	}

	public class MailThread implements Runnable {
		private MessageDetailBean msgDetailBean;
		
		public MailThread(MessageDetailBean msgDetailBean) {
			this.msgDetailBean = msgDetailBean;
		}
		
		@Override
		public void run() {
			// 发送邮件
			IMailSender mailSender = new SimpleMailSender();
			MailInfo mailInfo = new MailInfo();
			mailInfo.setUserName(ErProperties.getProperty(Constants.PROPERTY_MAIL_FROM));
			mailInfo.setFromAddress(ErProperties.getProperty(Constants.PROPERTY_MAIL_FROM));
			mailInfo.setPassword(ErProperties.getProperty(Constants.PROPERTY_MAIL_PASSWORD));
			mailInfo.setToAddress(ErProperties.getProperty(Constants.PROPERTY_MAIL_TO));
			mailInfo.setMailServerHost(ErProperties.getProperty(Constants.PROPERTY_MAIL_HOST));
			mailInfo.setMailServerPort(ErProperties.getProperty(Constants.PROPERTY_MAIL_PORT));
			mailInfo.setContent(this.msgDetailBean.getMsgContent());
			mailSender.sendTextMail(mailInfo);
		}
	}
}
